/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package games.kamisado;

import minmax.GameOutcome;
import games.KamisadoSquare;
import games.KamisadoState;
import java.util.ArrayList;
import java.util.Iterator;
import games.KamisadoState.KamisadoMove;
import games.SquareColor;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;

public class KamisadoOutcomeTest {

    @Test
    public void outcome_minplayerwins_returnsLastPlayerAsWinner() throws FileNotFoundException, IOException {
       KamisadoState state = new KamisadoState();
       TwoStepLossForMax(state);
       
       GameOutcome outcome = state.outcome();
       
       assertEquals(GameOutcome.Second, outcome);
    }
    
    @Test
    public void evaluate_maxplayerwins_returnsMaxValue() throws FileNotFoundException, IOException {
       KamisadoState state = new KamisadoState();
       TwoStepLossForMax(state);
       
       GameOutcome outcome = state.outcome();
       
       assertEquals(Integer.MAX_VALUE - 1, state.evaluate(true));
    }

    private void TwoStepLossForMax(KamisadoState state) {
            KamisadoSquare cell = state.cell(4, 7);
        KamisadoMove move = new KamisadoMove(4, 1, 4, 7, cell.getTower());
        state.move(move);
        
        KamisadoSquare cell2 = state.cell(1, 0);
        KamisadoMove move2 = new KamisadoMove(6, 5, 1, 0, cell2.getTower());
        state.move(move2);

        state.move(new KamisadoMove(4, 1, 4, 1, state.getTower(4, 1)));
        
        KamisadoMove move3 = new KamisadoMove(4, 7, 6, 5, state.getTower(6, 5));
        state.move(move3);
    }
    
    
    @Test
    public void outcome_deadlock_isDetectedAndCausingPlayerLose() {
       KamisadoState state = new KamisadoState();
       MoveBuilder builder = new MoveBuilder(state);
       
       state.move(builder.y(7, 6).x(5, 6).build()); //p1
       state.move(builder.y(0, 5).x(0, 5).build()); //p2
       
       state.move(builder.y(7, 3).x(7, 7).build());
       state.move(builder.y(0, 5).x(4, 4).build());
       
       state.move(builder.y(7, 3).x(2, 2).build());
       state.move(builder.y(0, 5).x(1, 6).build());
       
       state.move(builder.y(7, 5).x(4, 2).build());
       state.move(builder.y(0, 2).x(7, 7).build());

       state.move(builder.y(7, 6).x(6, 7).build());
       state.move(builder.y(0, 2).x(5, 3).build());

       state.move(builder.y(3, 2).x(2, 1).build());
       state.move(builder.y(0, 1).x(3, 4).build());

       state.move(builder.y(6, 4).x(7, 7).build());
       state.move(builder.y(1, 2).x(4, 4).build());

       state.move(builder.y(6, 5).x(6, 7).build());
       state.move(builder.y(0, 4).x(6, 6).build()); 
       
       //p2 makes the last move the rest will be a deadlock between 
       /* Player 1 [mikael] moves:
        KamisadoMove{y=5, x=7, oldX=7, oldY=5, tower=Tower{color=RED, player=false}}
        Player 2 [computer] moves:
        KamisadoMove{y=4, x=6, oldX=6, oldY=4, tower=Tower{color=BLUE, player=true}} */
       
       GameOutcome outcome = state.outcome();
       
       assertEquals(GameOutcome.First, outcome);
    }
    
    

    
}
